#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "std_msgs/msg/u_int32.hpp"
#include <iostream>

#include <chrono>
#include <functional>
#include <memory>
#include <string>

#include <rclcpp_components/register_node_macro.hpp>
#include "sensor_msgs/msg/image.hpp"
#include <sensor_msgs/msg/image.hpp>
#include <cv_bridge/cv_bridge.h>

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/calib3d.hpp"
#include <opencv2/highgui.hpp>

int n = 1;
using std::placeholders::_1;
using std::placeholders::_2;

class AuthorNode : public rclcpp::Node
{

public:
    AuthorNode(std::string name) : Node(name), count_(0)
    {
        RCLCPP_INFO(this->get_logger(), "大家好,我是四年级老师%s.", name.c_str());
        // pub_novel_ = this->create_publisher<std_msgs::msg::String>("book_dazai", 10);
        // timer_ = this->create_wall_timer(std::chrono::milliseconds(1000), std::bind(&AuthorNode::timerCallback, this));
        // sub_money_ = this->create_subscription<std_msgs::msg::UInt32>("beautiful_girl_money", 10, std::bind(&AuthorNode::topicCallback, this, _1));

        image_dir_ = "data/image.png";

        publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
        image_publisher_ = this->create_publisher<sensor_msgs::msg::Image>("image_raw", 10);
        cv::Mat img;
        img = cv::imread(image_dir_, cv::IMREAD_UNCHANGED);
        cv_bridge::CvImage cv_image;
        cv_image = cv_bridge::CvImage(image_.header, sensor_msgs::image_encodings::BGR8, img);
        cv_image.toImageMsg(image_);
        image_.header.frame_id = "base_link";
        timer_ = this->create_wall_timer(500ms, std::bind(&MinimalPublisher::timerCallback, this));
    }

private:
    std::string image_dir_;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub_novel_;
    rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr image_publisher_;
    // rclcpp::Subscription<std_msgs::msg::UInt32>::SharedPtr sub_money_;

    sensor_msgs::msg::Image image_;

    rclcpp::TimerBase::SharedPtr timer_;

    size_t count_;

    void timerCallback()
    {
        // std_msgs::msg::String message;
        // message.data = "Eight Scenes of Tokyo";
        // RCLCPP_INFO(this->get_logger(), "dazai的书籍'%s'(%d)已寄出!", message.data.c_str(), n++);
        // pub_novel_->publish(message);

        RCLCPP_INFO(this->get_logger(), "I set a image");

        image_.header.stamp = this->now();
        image_publisher_->publish(image_);
    };

    // void topicCallback(const std_msgs::msg::UInt32::SharedPtr msg)
    // {
    //     RCLCPP_INFO(this->get_logger(), "我已经收到%d元的稿费", msg->data);
    // };
};

int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<AuthorNode>("Li");
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}
